<script>
// 引入swiper插件文件
import Swiper from "swiper/js/swiper.min.js";
import "swiper/css/swiper.min.css";

// 引入接口
import {
  getBookcabinetJsonData,
  getBookcabinetData,
} from "/assets/api/bookcabinet.js";

// 引入选项式api
import { ref, onBeforeMount, reactive, onUpdated } from "vue";
import { Toast } from "vant";
import { useRouter } from "vue-router";
export default {
  setup() {
    // 定义请求接口数据结构
    let listObj = reactive({
      current: 0,
      allList: [],
      allList_temp: [],
      tagList: [],
      target: null,
      tagList_temp: [],
    });

    let renderFlag = ref(false);

    let router = useRouter();

    // 初始化swiper插件
    let swiperInit = () => {
      if (!listObj.target) {
        renderFlag.value = false;
        listObj.target = new Swiper(".my-swiper", {
          effect: "coverflow",
          grabCursor: true,
          centeredSlides: true,
          slidesPerView: "auto",
          coverflow: {
            rotate: 50,
            stretch: 25,
            depth: 100,
            modifier: 1,
            slideShadows: true,
          },
          on: {
            slideChangeTransitionEnd: function () {
              let index = this.activeIndex;
              // console.log("改变了，activeIndex为" + index);
              listObj.current = index;
            },
          },
        });
      } else if (renderFlag.value && listObj.target) {
        renderFlag.value = false;
        listObj.target.update();
      }
    };

    // 底部过滤器弹窗显示状态
    let footShow = ref(false);

    // 显示过滤器方法
    let showFootFilterFun = () => {
      footShow.value = true;
    };

    // 数据初始化
    let dateInit = async () => {
      let data = await getBookcabinetData().catch(() =>
        console.log("接口调用失败")
      );
      if (!data) data = await getBookcabinetJsonData();

      // console.log(data);
      listObj.allList = JSON.parse(JSON.stringify(data.allList));
      listObj.allList_temp = JSON.parse(JSON.stringify(data.allList));

      listObj.allList.forEach((item) => {
        item.tagList.forEach((tag) => {
          if (!listObj.tagList.some((o) => o == tag) && tag != "")
            listObj.tagList.push(tag);
        });
      });
      // console.log(listObj.tagList);
    };

    // 过滤器
    let filters_simplify = (val) => {
      if (val > 10000) return (val / 10000).toFixed(1) + "w";
      if (val > 1000) return (val / 1000).toFixed(1) + "k";

      return val;
    };

    // 标签点击事件
    let tagClickFun = (tag) => {
      let index = listObj.tagList_temp.findIndex((_tag) => tag == _tag);
      if (listObj.tagList_temp.length < 3) {
        if (index == -1) listObj.tagList_temp.push(tag);
        else listObj.tagList_temp.splice(index, 1);
      } else if (listObj.tagList_temp.length == 3) {
        if (index != -1) listObj.tagList_temp.splice(index, 1);
        else Toast("最多选三个标签");
      }
    };

    // 重置按钮
    let resetFun = () => {
      listObj.tagList_temp = [];
      Toast({
        message: "清空",
        position: "top",
      });
      console.log("清空");
    };

    // 确定按钮
    let subFun = () => {
      // 隐藏过滤器
      footShow.value = false;

      // 重新计算显示数组
      console.log(
        listObj.allList.filter((item) =>
          listObj.tagList_temp.every((tag) =>
            item.tagList.some((_tag) => _tag == tag)
          )
        )
      );
      listObj.allList_temp = listObj.allList.filter((item) =>
        listObj.tagList_temp.every((tag) =>
          item.tagList.some((_tag) => _tag == tag)
        )
      );
      if (!listObj.allList_temp.length) {
        listObj.allList_temp = listObj.allList;
        Toast({ message: "为找到相关\n为你推荐热门", position: "bottom" });
      }
      listObj.current = 0; //重置下标
      renderFlag.value = true; //隐藏
    };

    let filterShow = ref(false);
    // 挂载前
    onBeforeMount(() => {
      // 数据初始化
      dateInit();
      if (listObj.tagList_temp.length > 0) {
        filterShow.value = true;
      } else {
        filterShow.value = false;
      }
    });

    // 更新后
    onUpdated(() => {
      // 再次初始化插件
      swiperInit();
      if (listObj.tagList_temp.length > 0) {
        filterShow.value = true;
      } else {
        filterShow.value = false;
      }
    });

    // 详情页跳转
    let details = (id) => {
      router.push({ path: "/pages/DetailsView", query: { id: id } });
    };

    return {
      // 显示过滤器方法
      showFootFilterFun,

      // 底部过滤器弹窗显示状态
      footShow,

      // 数据
      listObj,

      // 过滤器
      filters_simplify,

      // 标签点击事件
      tagClickFun,

      // 重置按钮
      resetFun,

      // 确定按钮
      subFun,

      // 点击进入详情页
      details,
      filterShow,
    };
  },
};
</script>

<template>
  <div class="BookCabinet" v-if="listObj.allList_temp.length">
    <div class="head">
      <div @click="showFootFilterFun">
        <span class="iconfont icon-fliter" :class="{ con: filterShow }"></span>
      </div>
      <div class="title">
        {{ listObj.allList_temp[listObj.current].BookName }}
      </div>
      <div class="indicate">
        <span>{{ listObj.current + 1 }}</span
        ><em>/</em>{{ listObj.allList_temp.length }}
      </div>
    </div>
    <div class="body">
      <div
        class="bg"
        :style="{
          'background-image': `url(${
            listObj.allList_temp[listObj.current].BookImage
          })`,
        }"
      ></div>
      <div class="swiper-container my-swiper">
        <div class="swiper-wrapper">
          <div
            class="swiper-slide"
            v-for="item in listObj.allList_temp"
            :key="item.BookId"
            @click="details(item.BookId)"
          >
            <div class="img"><img :src="item.BookImage" alt="" /></div>

            <div class="box">
              <div class="title">{{ item.Author }}</div>

              <div class="content text-ellipsis-7">
                {{ item.Description }}
              </div>

              <div class="info">
                <div class="name">
                  <span>{{ item.BookName }}</span>
                </div>
                <div class="num">
                  <div class="sentiment">
                    {{ filters_simplify(item.sentiment) }}人气
                  </div>
                  <div class="wordsTotal">
                    {{ filters_simplify(item.wordsTotal) }}字
                  </div>
                </div>
              </div>
              <div class="btn">查看详情</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="foot">
      <van-popup v-model:show="footShow" position="bottom">
        <div class="filters-box">
          <div class="btns">
            <div class="reset" @click="resetFun">重置</div>
            <div class="sub" @click="subFun">确定</div>
          </div>
          <div class="category-box">
            <div class="category">
              <div class="category-item" v-if="listObj.tagList">
                <div class="title">标签大全</div>
                <div class="content">
                  <span
                    :class="{
                      con: listObj.tagList_temp.some((_tag) => _tag == tag),
                    }"
                    v-for="tag in listObj.tagList"
                    @click="tagClickFun(tag)"
                    :key="tag"
                  >
                    {{ tag }}
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </van-popup>
    </div>

    <BottomNavigationBar current="2" />
  </div>
</template>

<style lang="scss" scoped>
.BookCabinet {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;

  .head {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 50px;
    font-weight: bold;
    color: #333;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: rgba($color: #fff, $alpha: 0.9);
    z-index: 10;
    padding: 0 15px;
    .icon-fliter {
      font-size: 20px;
      &.con {
        color: #ea5588;
      }
    }
    .indicate {
      height: 25px;
      display: flex;
      justify-content: center;
      align-items: center;
      width: max-content;
      color: #000000;
      span {
        font-weight: bold;
        font-size: 22px;
      }
      em {
        margin: 0 4px;
      }
    }

    .title {
      position: absolute;
      width: max-content;
      max-width: calc(50% - 70px);
      left: 0;
      right: 0;
      margin: auto;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }

  .body {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    padding-top: 50px;
    box-sizing: border-box;

    .bg {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      top: 0;
      background: url(/assets/img/page1.png) no-repeat center;
      background-size: 130%;
      filter: blur(10px);
      box-shadow: 0 0 120px 60px rgba($color: #000000, $alpha: 0.6) inset;
    }

    .my-swiper {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 54px;
      top: 50px;

      .swiper-wrapper {
        // padding: 0 20px;
        // box-sizing: border-box;
        .swiper-slide {
          background-color: #fff;
          border-radius: 10px;
          margin: 30px 4px;
          width: 280px;
          height: calc(100% - 60px);
          overflow: hidden;

          .img {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            z-index: -1;
          }

          .box {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            max-height: 60%;
            background-image: linear-gradient(transparent 0%, #fff 10%);

            .title {
              text-align: center;
              height: 30px;
              font-weight: bold;
              color: #1c1c1c;
              font-size: 14px;
              line-height: 26px;
            }

            .content {
              padding: 0 20px;
              line-height: 20px;
              text-indent: 2em;
              letter-spacing: 1px;
              color: #434343;
              font-size: 13px;
            }

            .info {
              // display: flex;
              // height: 22px;
              // align-items: center;
              // justify-content: space-between;
              padding: 0 20px;
              margin-top: 5px;
              margin-bottom: -10px;
              .name {
                color: #f09335;
                display: flex;
                align-items: center;
                margin-bottom: 5px;
                span {
                  display: block;
                  // max-width: 4em;
                  overflow: hidden;
                  text-overflow: ellipsis;
                  white-space: nowrap;
                }
              }
              .num {
                display: flex;
                align-items: center;
                .sentiment {
                  border-radius: 10px;
                  height: 22px;
                  border: 1px solid #ececec;
                  color: #898989;
                  padding: 0 10px;
                  font-size: 12px;
                  box-sizing: border-box;
                  transform: scale(0.9);
                }
                .wordsTotal {
                  border-radius: 10px;
                  height: 22px;
                  border: 1px solid #f6eae0;
                  color: #f3c592;
                  padding: 0 10px;
                  font-size: 12px;
                  box-sizing: border-box;
                  transform: scale(0.9);
                }
              }
            }

            .btn {
              color: #eee;
              height: 30px;
              display: flex;
              align-items: center;
              justify-content: center;
              box-sizing: border-box;
              font-size: 13px;
              background-color: #ea5588;
              border-radius: 15px;
              margin: 20px;
            }
          }
        }
        .small_img {
          height: 46px;
          width: 40px;
          position: absolute;
          right: 9px;
          top: 26px;
        }
        .big_img {
          height: 67px;
          width: 50px;
          position: absolute;
          right: 21px;
          top: 16px;
          box-shadow: 2px 2px 5px 0px #928d8d;
        }
      }
    }
  }

  .foot {
    .filters-box {
      // padding: 10px;
      .btns {
        height: 40px;
        padding: 0 10px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px solid #f7f7f7;
        .reset {
          color: #424242;
          &:active {
            color: #000;
            transform: scale(1.08);
          }
        }
        .sub {
          color: #fcfefd;
          background-color: #e91e63;
          width: 80px;
          height: 30px;
          border-radius: 4px;
          text-align: center;
          line-height: 30px;
          &:active {
            opacity: 0.9;
          }
        }
      }
      .category-box {
        height: 300px;
        overflow-y: scroll;
        overflow-x: hidden;
        .category-item {
          padding: 0 10px;
          .title {
            color: #616161;
            font-weight: bold;
            height: 40px;
            line-height: 40px;
          }
          .content {
            display: flex;
            flex-wrap: wrap;
            padding-bottom: 54px;
            span {
              padding: 0 14px;
              color: #696969;
              font-size: 12px;
              height: 26px;
              line-height: 26px;
              border-radius: 5px;
              background-color: #f0f0f0;
              margin-right: 12px;
              margin-bottom: 12px;
              &.con {
                background-color: #ea5588;
                color: #fff;
              }
            }
          }
        }
      }
    }
  }
}
</style>